TGGS Computer Science

Spring 1 - Lesson 8 - Revision Lesson

🌟 Learning Objectives

🌟 We are learning to consolidate our understanding of Python


βœ… Outcomes

  • We will consolidate our understanding of inputs, outputs and variables
  • We will consolidate our understanding of Selection IF and ELIF
  • We will consolidate our understanding of While loops and For Loops
  • We will consolidate our understanding of Lists (1D and 2D)
🐍 Activity 1 – Inputs, Outputs and Variables

🐍 Activity 1.1 - Assign an input to a variable and output it

Your task:

  1. Create a variable called sport
  2. Use input() to ask the user for their favourite sport
  3. Store the user’s answer in the variable
  4. Use print() to output a sentence saying that you also like that sport
  5. Make sure the sport name appears in the output
Click to see how this should look
sport = input("Input your favourite sport: ")
print(f"Wow, I really like {sport} as well!")

🐍 Activity 1.2 Casting an input to an integer and multiplying it

Your task:

  1. Create a variable called num
  2. Use input() to ask the user for a number
  3. Convert the input into an integer using int()
  4. Multiply the number by 2
  5. Output the result using print()
Click to see how this should look
num = int(input("Enter a number you wish to multiply by 2: "))
print(num * 2)
🐍 Activity 2 – Selection and Branching Selection

🐍 Activity 2.1 - Working with selection (IF / ELSE)

Your task:

  1. Create a variable called capital
  2. Ask the user to enter the capital city of Poland
  3. Use an if / else statement to check the answer
  4. Compare the answer to "warsaw" using .lower()
  5. Output Correct! if the answer is right
  6. Output Incorrect if the answer is wrong
Click to see how this should look
capital = input("Name the capital of Poland: ")
if capital.lower() == "warsaw":
    print("Correct!")
else:
    print("Incorrect")

🐍 Activity 2.2 - Working with selection (IF / ELIF / ELSE)

Your task:

  1. Create a variable called colour
  2. Ask the user to enter a primary colour
  3. Use an if / elif / else statement
  4. Check for red, blue, or yellow
  5. Use .lower() when comparing values
  6. Output Correct! for a primary colour
  7. Output Incorrect for any other answer
Click to see how this should look
colour = input("Name a primary colour: ")
if colour.lower() == "red":
    print("Correct!")
elif colour.lower() == "blue":
    print("Correct!")
elif colour.lower() == "yellow":
    print("Correct!")
else:
    print("Incorrect")
🐍 Activity 3 – While loops

🐍 Activity 3.1 - Working with a conditional while loop

Your task:

  1. Create a variable called password
  2. Ask the user to enter the password
  3. Use a while loop that runs while the password is not Secret123
  4. Inside the loop:
    • Tell the user the password is incorrect
    • Ask them to enter the password again
  5. After the loop ends, output Password accepted
Click to see how this should look
password = input("Enter the password: ")

while password != "Secret123":
    print("Password is incorrect, please try again: ")
    password = input("Enter the password: ")
    
print("Password accepted")

🐍 Activity 3.2 - Working with a while True loop

Your task:

  1. Start a while True loop
  2. Ask the user to enter a primary colour
  3. Store the answer in a variable called guess
  4. Use if / elif / else to check for:
    • red
    • yellow
    • blue
  5. If the answer is correct:
    • Output Correct
    • Use break to stop the loop
  6. If the answer is incorrect:
    • Output Incorrect
    • Allow the loop to continue
Click to see how this should look
while True:
    guess = input("Input a primary colour: ")
    
    if guess.lower() == "red":
        print("Correct")
        break
    elif guess.lower() == "yellow":
        print("Correct")
        break
    elif guess.lower() == "blue":
        print("Correct")
        break
    else:
        print("Incorrect")
🐍 Activity 4 – For Loops and Lists

🐍 Activity 4.1 - For loop to output the 8 times table

Your task:

  1. Create a for loop that runs from 1 to 12
  2. In each loop:
    • Multiply the loop variable by 8
    • Output the calculation in a clear sentence
  3. Use an f-string to format the output

Example output:

1 X 8 = 8
2 X 8 = 16
3 X 8 = 24
Click to see how this should look
for i in range(1, 13):
    print(f"{i} X 8 = {i * 8}")

🐍 Activity 4.2 - For loop through a 1 dimensional list

Your task:

  1. Use the list of Olympic sports provided
  2. Create a for loop that runs through every item in the list
  3. Use the loop counter to access each sport
  4. Output a sentence stating that each sport will feature in the 2028 Olympics
  5. Use an f-string to format the output

Given list:

sports = ["Shotput", "Javelin", "Triple Jump", "High Jump", "Discus", "Diving"]
Click to see how this should look
#index       0           1            2             3           4        5
sports = ["Shotput", "Javelin", "Triple Jump", "High Jump", "Discus", "Diving"]

for i in range(0, 6):
    print(f"{sports[i]} will feature in the 2028 Olympics")
🐍 Activity 5 – Outputting data from 2 Dimensional Lists

🐍 Activity 5.1 - Output both items in each record

  • You have been given a 2 dimensional list comprised of the following records
  • landmarks = [
        ["Eiffel Tower", "Paris"],
        ["Taj Mahal", "Agra"],
        ["Forbidden Palace", "Beijing"],
        ["Golden Gate Bridge", "San Francisco"],
        ["Royal Mile", "Edinburgh"],
        ["Burj Khalifa", "Dubai"],
        ["Hagia Sophia", "Istanbul"],
        ["Sagrada Familia", "Barcelona"]
        ]
    
  • Write a for loop, where the range will successfully navigate all records
  • During each iteration, output the first and second item of whichever record matches the loop counter variable (i)
  • Each output should be put together in an appropriate sentence
  • The Eiffel Tower is in Paris
    The Taj Mahal is in Agra
    The Forbidden Palace is in Beijing
    
    etc....
    

Click to see how this should look

landmarks = [
    ["Eiffel Tower", "Paris"],
    ["Taj Mahal", "Agra"],
    ["Forbidden Palace", "Beijing"],
    ["Golden Gate Bridge", "San Francisco"],
    ["Royal Mile", "Edinburgh"],
    ["Burj Khalifa", "Dubai"],
    ["Hagia Sophia", "Istanbul"],
    ["Sagrada Familia", "Barcelona"]
    ]

for i in range(0, 8):
    print(f"The {landmarks[i][0]} is in {landmarks[i][1]}")
    

🐍 Activity 5.2 - Searching a 2 Dimensional List

Your task:

  1. Use the list of athletes and medals provided
  2. Create a variable called search
  3. Ask the user to enter a medal type:
    • bronze
    • silver
    • gold
  4. Create a for loop that runs through every record
  5. In each loop:
    • Check if the medal matches the user’s search
    • If it matches, output the athlete’s name and medal

Given list:

medals = [
    ["Keely Hodgkinson", "gold"],       
    ["Adam Peaty", "gold"],             
    ["Duncan Scott", "silver"],         
    ["Ben Proud", "silver"],           
    ["Tom Daley", "bronze"],            
    ["Bradley Wiggins", "bronze"],      
    ["Jessica Ennis-Hill", "gold"],    
    ["Christine Ohuruogu", "bronze"] 
]
Click to see how this should look
search = input("Input a type of medal (bronze, silver or gold): ")

for i in range(0, 8):
    if medals[i][1] == search.lower():
        print(f"{medals[i][0]} won {medals[i][1]}")